from mcp.server.fastmcp import FastMCP
from DrissionPage import Chromium, ChromiumOptions, SessionOptions

mcp = FastMCP("Weather")

@mcp.tool()
async def get_weather(location: str) -> str:
    """Get weather for location."""
    return "It's always sunny in New York"

@mcp.tool()
async def open_webpage(url: str) -> str:
    """使用drissionpage打开网页, 当用户的输入中包含有网页链接时，使用该工具打开这个链接

    Args:
        url (str): The webpage URL to open

    Returns:
        str: Success message or error description
    """
    try:
        co = ChromiumOptions(read_file=False)  # 不读取文件方式新建配置对象
        so = SessionOptions(read_file=False)
        tab = Chromium(addr_or_opts=co, session_options=so).latest_tab

        # 打开合作伙伴平台页面
        tab.get(url)
        return f"Successfully opened webpage: {url}"
    except Exception as e:
        return f"Failed to open webpage: {str(e)}"
    finally:
        # 可以选择是否关闭页面
        # page.close()
        pass



if __name__ == "__main__":
    mcp.run(transport="streamable-http")
    # mcp.run(transport="stdio")


